<!-- from 7 5 2 7 1 8 9 2 0 -->
<template>
  <a-row :gutter="24">
    <a-col :md="24">
      <a-card :style="cardStyle" :bordered="false">
        <!-- 查询区域 -->
        <div class="table-page-search-wrapper">
          <a-form layout="inline" @keyup.enter.native="searchQuery">
            <a-row :gutter="24">
              <a-col :md="6" :sm="24">
                <a-form-item :label="$t('reportQuery.documentNo')" :labelCol="labelCol" :wrapperCol="wrapperCol">
                  <a-input :placeholder="$t('reportQuery.pleaseEnterTheDocumentNumber')" v-model="queryParam.number"></a-input>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item :label="$t('reportQuery.productInformation')" :labelCol="labelCol" :wrapperCol="wrapperCol">
                  <a-input :placeholder="$t('reportQuery.barcodeModel')" v-model="queryParam.materialParam"></a-input>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item :label="$t('reportQuery.documentDate')" :labelCol="labelCol" :wrapperCol="wrapperCol">
                  <a-range-picker
                    style="width: 100%"
                    v-model="queryParam.createTimeRange"
                    :default-value="defaultTimeStr"
                    format="YYYY-MM-DD"
                    :placeholder="[$t('reportQuery.startTime'), $t('reportQuery.endTime')]"
                    @change="onDateChange"
                  />
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24" >
                <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons table-operator">
                  <a-button type="primary" @click="searchQuery">{{$t('manuManagement.query')}}</a-button>
                  <ColumnsSetting style="ma" @columnsChange="columnsChange" :columns="columns"></ColumnsSetting>
                  <a-button v-if="btnEnableList.indexOf(5) > -1"  style="margin-left: 8px" @click="reportPrint" icon="printer">{{$t('reportQuery.print')}}</a-button>
                  <a-button v-if="btnEnableList.indexOf(5) > -1"  style="margin-left: 8px" @click="generatePrintData" icon="printer">{{$t('inPrint.other') + $t('reportQuery.print')}}</a-button>
                  <a-button v-if="btnEnableList.indexOf(3) > -1" :loading="downLoading" style="margin-left: 8px" @click="exportExcel" icon="download">{{$t('reportQuery.export')}}</a-button>
                 <a @click="handleToggleSearch" style="margin-left: 8px">
                    {{ toggleSearchStatus ? $t('reportQuery.putAway'): $t('reportQuery.open') }}
                    <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
                  </a>
                </span>
              </a-col>
              <template v-if="toggleSearchStatus">
                <a-col :md="6" :sm="24">
                  <a-form-item :label="$t('reportQuery.supplier')" :labelCol="labelCol" :wrapperCol="wrapperCol">
                    <a-select :placeholder="$t('reportQuery.selectSupplier')" v-model="queryParam.organId"
                              :dropdownMatchSelectWidth="false" showSearch allow-clear optionFilterProp="children">
                      <a-select-option v-for="(item,index) in supList" :key="index" :value="item.id">
                        {{ item.supplier }}
                      </a-select-option>
                    </a-select>
                  </a-form-item>
                </a-col>
                
              <a-col :md="6" :sm="24">
                <a-form-item :label="$t('commodityManagement.category')" :labelCol="labelCol" :wrapperCol="wrapperCol">
                  <a-tree-select style="width:100%" :dropdownStyle="{ maxHeight: '200px', overflow: 'auto' }"
                  :filterTreeNode="filterTreeNode"
                  showSearch
                    allow-clear :treeData="categoryTree" v-model="queryParam.categoryId"
                    :placeholder="$t('commodityManagement.pleaseCategory')">
                  </a-tree-select>
                </a-form-item>
              </a-col>
                <a-col :md="6" :sm="24">
                  <a-form-item :label="$t('defaultTranslate.operator')" :labelCol="labelCol" :wrapperCol="wrapperCol">
                    <a-select :placeholder="$t('defaultTranslate.operator')" allowClear v-model="queryParam.creator">
                      <a-select-option v-for="(item,index) in userList" :key="index" :value="item.id">
                      {{ item.userName }}
                        </a-select-option>
                      </a-select>
                  </a-form-item>
                </a-col> 
                <a-col :md="6" :sm="24">
                  <a-form-item :label="$t('otherJS.DocumentType')"  :labelCol="labelCol" :wrapperCol="wrapperCol">
                    <a-select :placeholder="$t('otherJS.DocumentType')" allowClear v-model="queryParam.stype" @change="subTypeChange">
                      <a-select-option v-for="(item,index) in subTypes" :key="index" :value="item.value">
                        {{ item.label }}
                      </a-select-option>
                      </a-select>
                  </a-form-item>
                </a-col>  
                
              <a-col :md="6" :sm="24" v-show="false">
                <a-form-item :label="$t('reportQuery.productInformation')" :labelCol="labelCol" :wrapperCol="wrapperCol">
                  <a-input :placeholder="$t('reportQuery.barcodeModel')" v-model="queryParam.otherStatus"></a-input>
                </a-form-item>
              </a-col>
                <a-col :md="6" :sm="24">
                  <a-form-item :label="$t('reportQuery.warehouse')" :labelCol="labelCol" :wrapperCol="wrapperCol">
                    <a-select
                      optionFilterProp="children"
                      :dropdownMatchSelectWidth="false"
                      showSearch allow-clear style="width: 100%"
                      :placeholder="$t('reportQuery.pleaseSelectWarehouse')"
                      v-model="queryParam.depotId">
                      <a-select-option v-for="depot in depotList" :value="depot.id" :key="depot.id">
                        {{ depot.depotName }}
                      </a-select-option>
                    </a-select>
                  </a-form-item>
                </a-col>
                <a-col :md="6" :sm="24">
                  <a-form-item :label="$t('reportQuery.remarks')" :labelCol="labelCol" :wrapperCol="wrapperCol">
                    <a-input :placeholder="$t('reportQuery.pleaseEnterComments')" v-model="queryParam.remark"></a-input>
                  </a-form-item>
                </a-col>
              </template>
            </a-row>
          </a-form>
        </div>
        <!-- table区域-begin -->
        <section ref="print" id="reportPrint">
          <a-table
            bordered
            ref="table"
            size="middle"
            :columns="columns"
            :dataSource="dataSource"
            :pagination="false"
            :scroll="scroll"
            :loading="loading"
            @change="handleTableChange">
            <span slot="numberCustomRender" slot-scope="text, record">
              <a @click="myHandleDetail(record)">{{record.number}}</a>
            </span>
          </a-table>
          <a-row :gutter="24" style="margin-top: 8px;text-align:right;">
            <a-col :md="24" :sm="24">
              <a-pagination @change="paginationChange" @showSizeChange="paginationShowSizeChange"
                size="small"
                show-size-changer
                :showQuickJumper="true"
                :current="ipagination.current"
                :page-size="ipagination.pageSize"
                :page-size-options="ipagination.pageSizeOptions"
                :total="ipagination.total"
                :show-total="(total, range) => $t('reportQuery.common') +`${total-Math.ceil(total/ipagination.pageSize)}`+ $t('reportQuery.strip')">
                <template slot="buildOptionText" slot-scope="props">
                  <span>{{ props.value-1 }}{{$t('reportQuery.strip')}}/{{$t('reportQuery.page')}}</span>
                </template>
              </a-pagination>
            </a-col>
          </a-row>
        </section>
        <!-- table区域-end -->
        <!-- 表单区域 -->
        <bill-detail ref="modalDetail"></bill-detail>
      </a-card>
    </a-col>
    <saleOutRepotModual ref="saleOutRepotModual" :list="dataSource" :columns="columns" :queryParam="queryParam"></saleOutRepotModual>
  </a-row>
</template>
<script>
  import BillDetail from '../bill/dialog/BillDetail'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import { getNowFormatYear, openDownloadDialog, sheet2blob} from "@/utils/util"
  import {getAction} from '@/api/manage'
  import {findBySelectSup, findBillDetailByNumber,getUserList} from '@/api/api'
  import JEllipsis from '@/components/jeecg/JEllipsis'
  import moment from 'moment'
  import { titleObj,enumType,billDetailRemarks } from '@/lang/category'
  import {getLanCode} from '@/lang';
  import saleOutRepotModual from './modules/saleOutRepotModual';
  export default {
    name: "InDetail",
    mixins:[JeecgListMixin],
    components: {
      BillDetail,
      JEllipsis,
      saleOutRepotModual
    },
    data () {
      return {
        labelCol: {
          span: 5
        },
        wrapperCol: {
          span: 18,
          offset: 1
        },
        userList:[],
        subTypes:[],
        // 查询条件
        queryParam: {
          organId: '',
          number: '',
          materialParam:'',
          depotId: '',
          beginTime: getNowFormatYear() + '-01-01',
          endTime: moment().format('YYYY-MM-DD'),
          type: '入库',
          remark: '',
          subType:'',
          creator:'',
        },
        ipagination:{
          pageSize: 11,
          pageSizeOptions: ['11', '21', '31', '101', '201']
        },
        dateFormat: 'YYYY-MM-DD',
        currentDay: moment().format('YYYY-MM-DD'),
        defaultTimeStr: '',
        supList: [],
        depotList: [],
        tabKey: "1",
        // 表头
        columns: [
          {
            title: this.$t('defaultTranslate.lineNumber'), dataIndex: 'rowIndex', width:40, align:"center",
            customRender:function (t,r,index) {
              return (t !== '合计') ? (parseInt(index) + 1) : t
            }
          },
          {
            title: this.$t('reportQuery.documentNo'), dataIndex: 'number', width: 100,
            scopedSlots: { customRender: 'numberCustomRender' },
          },
          {title:this.$t('reportQuery.barcode'), dataIndex: 'barCode', width: 80},
          {title:this.$t('reportQuery.name'), dataIndex: 'mname', width: 100},
          {title:this.$t('reportQuery.specification'), dataIndex: 'standard', width: 100},
          { title: this.$t('reportQuery.category'), dataIndex: 'categoryName',width:80 },
          {title:this.$t('reportQuery.model'), dataIndex: 'model', width: 60, ellipsis:true},
          {title:this.$t('reportQuery.unit'), dataIndex: 'mUnit', width: 60, ellipsis:true},
          {title:this.$t('reportQuery.color'), dataIndex: 'color', width: 60, ellipsis:true},
          {title:this.$t('reportQuery.quantity'), dataIndex: 'operNumber', sorter: (a, b) => a.operNumber - b.operNumber, width: 60},
          {title:this.$t('reportQuery.unitPrice'), dataIndex: 'unitPrice', sorter: (a, b) => a.unitPrice - b.unitPrice, width: 60},
          {title:this.$t('reportQuery.breakNumber'), dataIndex: 'custom11', width: 50},
          {title:this.$t('reportQuery.amount'), dataIndex: 'allPrice', sorter: (a, b) => a.allPrice - b.allPrice, width: 60},
          {title:this.$t('reportQuery.supplier'), dataIndex: 'sname', width: 80, ellipsis:true},
          {title:this.$t('reportQuery.warehouse'), dataIndex: 'dname', width: 80, ellipsis:true},
          {title:this.$t('reportQuery.warehousingDate'), dataIndex: 'operTime', width: 80},
          {title:this.$t('otherJS.DocumentType'), dataIndex: 'newType', width: 80,
            customRender:function (t,r,index) {
              let flag = getLanCode() === 'en'
              // let text = t.replace(/入库/g,'')
              return flag ? enumType[t] || t : t
            }
          },
          {title:this.$t('defaultTranslate.operator'), dataIndex: 'loginName', width: 80},
          {title:this.$t('reportQuery.remarks'), dataIndex: 'newRemark', width: 100, ellipsis:true,
            customRender:function (t,r,index) {
              let flag = getLanCode() === 'en'
              // let text = t.replace(/入库/g,'')
              return flag ? billDetailRemarks[t] || t : t
            }
        }
        ],
        url: {
          list: "/depotHead/findInDetail",
        }
      }
    },
    created () {
      this.getDepotData()
      this.initSupplier()
      this.initUser()
      this.initSubTypes()
      this.loadTreeData()
      this.defaultTimeStr = [moment(getNowFormatYear() + '-01-01', this.dateFormat), moment(this.currentDay, this.dateFormat)]
    },
    methods: {
      moment,
      getQueryParams() {
        this.queryParam.subType = this.queryParam.subType || '' 
        this.queryParam.creator = this.queryParam.creator || '' 
        this.queryParam.subType = this.queryParam.stype === '盘点报损报溢' ? '其它' : (this.queryParam.stype || '')
        let param = Object.assign({}, this.queryParam, this.isorter);
        param.field = this.getQueryField();
        param.currentPage = this.ipagination.current;
        param.pageSize = this.ipagination.pageSize-1;
        return param;
      },
      onDateChange: function (value, dateString) {
        console.log(dateString[0],dateString[1]);
        this.queryParam.beginTime=dateString[0];
        this.queryParam.endTime=dateString[1];
      },
      subTypeChange(value){
        let otherStatus = ''
        switch (value){
          case '其它':
          otherStatus = 1
            break;
          case '盘点报损报溢':
          otherStatus = 2
            break;
          default:
          otherStatus = ''
            break;
        }
        this.queryParam.otherStatus = otherStatus
      },
      initSupplier() {
        let that = this;
        findBySelectSup({}).then((res)=>{
          if(res) {
            that.supList = res;
          }
        });
      },
      getDepotData() {
        getAction('/depot/findDepotByCurrentUser').then((res)=>{
          if(res.code === 200){
            this.depotList = res.data;
          }else{
            this.$message.info(res.data);
          }
        })
      },
      myHandleDetail(record) {
        findBillDetailByNumber({ number: record.number }).then((res) => {
          if (res && res.code === 200) {
            this.handleDetail(res.data, record.newType);
          }
        })
      },
      searchQuery() {
        if(this.queryParam.beginTime == '' || this.queryParam.endTime == ''){
          this.$message.warning(this.$t('reportQuery.pleaseDate'))
        } else {
          this.loadData(1);
        }
      },
      
    initUser () {
        getUserList({}).then((res) => {
          this.userList = res
        })
      },
      initSubTypes(){
        let arr = ['仓库退货','其它','采购','销售退货','零售退货','期初库存','盘点报损报溢']
        this.subTypes = arr.map(v=>{
          let isEn = getLanCode() === 'en' ? true : false
          let item= {
            label:isEn ? titleObj[v] || v : v,
            value:v
          }
          return item
        })
      },
      
        
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less'
</style>